<template>
    <div>
        <div class="background">
            <section class="header">
                <video autoplay loop class="void-background" muted plays-inlne>
                    <source src="../mv/G2s.mp4" type="video/mp4"/>
                    浏览器不支持 video 标签，建议升级浏览器。
                </video>
            </section>
        </div>
        <div class="card">
            <div>
                <h2>注 册</h2>
                <br><br>
            </div>
            <el-form :model="user" ref="registerForm" :rules="registerRule" :hide-required-asterisk="true" inline>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="user.name" placeholder="请输入姓名" style="width: 210px;"></el-input>
                </el-form-item>
                <el-form-item label="账号" prop="account">
                    <el-input v-model="user.account" placeholder="请输入账号" style="width: 210px;"></el-input>
                </el-form-item>
                <br>
                <el-form-item prop="gender" label="姓别">
                    <el-select v-model="user.gender" style="width: 210px;">
                        <el-option label="男" value="男"></el-option>
                        <el-option label="女" value="女"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="email" label="邮箱">
                    <el-input type="text" v-model="user.email" auto-complete="off"
                              placeholder="请输入邮箱" style="width: 210px;"></el-input>
                </el-form-item>
                <br>
                <el-form-item label="密码" prop="password">
                    <!--     @keypress.enter.native 按下回车键，调用注册方法     -->
                    <el-input v-model="user.password" placeholder="请输入密码" show-password
                              style="width: 210px;"></el-input>
                </el-form-item>
                <br>
                <el-form-item>
                    <el-button style="border-radius: 10px;" type="primary" @click="register">注册</el-button>
                    <el-button style="color: white;" type="text" @click="$router.push('/login')">已有账号？去登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>

    export default {
        name: "register",
        data() {
            return {
                user: {},
                registerRule: {
                    account: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                    ],
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur'}
                    ],
                    email: [
                        {required: true, message: '请输入邮箱', trigger: 'blur'},
                        {type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur']}
                    ],
                },
            }
        },
        methods: {
            register() {
                // 前端表单验证
                this.$refs.registerForm.validate((valid) => {
                    if (valid) {
                        console.log(this.user)
                        // 表单验证通过后，调用后端接口
                        this.$axios.post('/api/user/register', this.user).then(res => {
                            console.log(res)
                            if (res.data.status === 1) {
                                // 注册成功，调到登录页
                                this.$router.push(this.redirect || '/login');
                                this.$message.success(res.data.msg);
                            } else {
                                this.$message.error(res.data.msg);
                            }
                        })
                    } else {
                        this.$message.error('请填写完整内容');
                        return false;
                    }
                })
            }
        }
    }
</script>

<style scoped>
    @import "../css/register.css";

    .el-radio__label element.st {
        font-size: 35px;
    }
</style>